import os
import sys
path='.'
for file in os.listdir(path):
	if file.startswith('fmc_row'):
		if file.endswith('.txt'):
			name=file[7]
			f=open('fmc_row'+name+'.txt')
			lines=f.readlines()
			f.close()
			with open('fmc_row'+name+'.symdef','aw') as ff:
				ff.write('[labels]'+'\n')
				ff.write('Row '+name+'\n')
				ff.write('refdes=J?'+'\n')
				ff.write('FMC HPC'+'\n')
				ff.write('! author=Yilun Xu'+'\n')
				ff.write('! device='+'\n')
				ff.write('! footprint='+'\n')
				ff.write('\n')
			with open('fmc_row'+name+'.symdef','aw') as ff:
				ff.write('[right]'+'\n')
				for i in range(len(lines)):
					ff.write(lines[i])
	if file=='fmc_GND.txt':
		f=open('fmc_GND.txt')
		lines=f.readlines()
		f.close()
		for i in range(len(lines)):
			if lines[i].split()[0]=='A1':
				index1=i
			if lines[i].split()[0]=='E17':
				index2=i
			if lines[i].split()[0]=='E20':
				index3=i
			if lines[i].split()[0]=='ST2':
				index4=i
		with open('fmc_GND.symdef','aw') as ff:
			ff.write('[labels]'+'\n')
			ff.write('GND'+'\n')
			ff.write('refdes=J?'+'\n')
			ff.write('FMC HPC'+'\n')
			ff.write('! author=Yilun Xu'+'\n')
			ff.write('! device='+'\n')
			ff.write('! footprint='+'\n')
			ff.write('\n')
		with open('fmc_GND.symdef','aw') as ff:
			ff.write('[left]'+'\n')
			for i in range(len(lines)):
				if index3<=i<=index4:
					ff.write(lines[i])
			ff.write('\n')
		with open('fmc_GND.symdef','aw') as ff:
			ff.write('[right]'+'\n')
			for i in range(len(lines)):
				if index1<=i<=index2:
					ff.write(lines[i])

for file in os.listdir(path):
	if file.endswith('.symdef'):
		os.system('make %s' %(file[:-3]))
